﻿using System;
using DTT.AreaOfEffectRegions;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.Logic;
using LFloatMath.Math;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class LineToUnitRegion : LineRegionBase
    {
        [SerializeField] private Transform mLockTrans;
        [SerializeField] private Transform mLockBodyTrans;
        [SerializeField] private Transform mLinePointTrans;
        [SerializeField] private CircleRegion mRangeRegion;
        [SerializeField] public int targetRange = 2;
        
        private Transform mFrom;
        private Transform mTarget;
        private Vector3 mLineTarget;
        
        public float MaxLength = 0;

        private void LateUpdate()
        {
            if (mLockBodyTrans == null || mLockTrans == null)
                return;

            // Sets lock angle and position.
            if (this.mTarget)
            {
                var rTargetPos = this.mTarget.position;
                this.mLockTrans.position = rTargetPos;
                this.mLockTrans.LookAt(ManagerCenter.UI.MainCamera.transform.position);
            }

            var rFromPos = this.mFrom.position;
            // Sets body angle and scale.
            var rTargetRot = this.mLineTarget - rFromPos;
            if (rTargetRot != Vector3.zero)
            {
                this.mLockBodyTrans.rotation = Quaternion.LookRotation(rTargetRot); //= new Vector3(this.mLockBodyTrans.eulerAngles.x, Angle, this.mLockBodyTrans.eulerAngles.z);
            }
            this.mLockBodyTrans.position = (rFromPos + mLineTarget) / 2;
            this.mLockBodyTrans.localScale = new Vector3(1, this.mLockBodyTrans.localScale.y, Mathf.Max(Length, 0));
            this.mLinePointTrans.position = mLineTarget;
            this.mLinePointTrans.LookAt(ManagerCenter.UI.MainCamera.transform.position);
        }

        public void Initialize(Transform rFrom,  TargetSelect_InputUnit_ConfigData config)
        {
            this.mFrom = rFrom;
            this.MaxLength = new LFloat(true, config.range).ToFloat();
            if (this.mRangeRegion)
            {
                this.mRangeRegion.Radius = this.MaxLength;
            }
        }
        
        public void LineTarget(Vector3 dir)
        {
            var rFromPos = this.mFrom.position;
            this.mLineTarget = rFromPos + dir * this.MaxLength;
            this.Length = Vector3.Distance(this.mLineTarget, rFromPos);
        }

        public void LockTarget(Transform rTo)
        {
            this.mTarget = rTo;
            if (this.mLockTrans)
            {
                this.mLockTrans.gameObject.SetActive(this.mTarget);
                if (this.mTarget)
                {
                    var rTargetPos = this.mTarget.position;
                    this.Length = Vector3.Distance(rTargetPos, this.mFrom.position);
                    // this.mLineTarget = rTargetPos;
                }
            }            
        }
    }
}